package com.cx.core.redis.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import java.util.Set;

@Service
public class RankService {

    @Autowired
    private RedisTemplate redisTemplate;

    // 添加排名
    public boolean addRank(String rankKey, String member, double score) {
        ZSetOperations<String, String> zSetOps = redisTemplate.opsForZSet();
        return zSetOps.add(rankKey, member, score);
    }

    // 获取排名
    public Set<String> getRank(String rankKey, long start, long end) {
        ZSetOperations<String, String> zSetOps = redisTemplate.opsForZSet();
        return zSetOps.range(rankKey, start, end);
    }

    // 获取成员的排名
    public Long getMemberRank(String rankKey, String member) {
        ZSetOperations<String, String> zSetOps = redisTemplate.opsForZSet();
        return zSetOps.rank(rankKey, member);
    }

    // 获取成员的分数
    public Double getMemberScore(String rankKey, String member) {
        ZSetOperations<String, String> zSetOps = redisTemplate.opsForZSet();
        return zSetOps.score(rankKey, member);
    }
}
